package com.lovo.sqlatm;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Test01 {

    public static Connection getConnection() {
        //加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //创建连接
        Connection con = null;
        try {
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/atm?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "peng18383058225");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("con:" + con);
        return con;
    }


    public static void insert(String a_acconut, String a_password,  int a_balance) {
        // 创建连接
        Connection con = getConnection();
        Statement sta = null;
        // 获取sql语句对象
        try {
            sta = con.createStatement();
            String str = "insert INTO t_atm(a_account,a_password,a_balance) VALUES(" + a_acconut + ",'" + a_password + "','" + a_balance +  ")";
            //执行SQL语句
            int row = sta.executeUpdate(str);
            System.out.println("影响行数" + row + "行");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            close(con, sta);
        }
    }

    public static void update(String a_acc, String a_paswor,  int a_balan) {
        // 创建连接
        Connection con = getConnection();
        Statement sta = null;
        // 获取sql语句对象
        try {
            sta = con.createStatement();
            String str = "update t_atm set a_account='"+a_acc+"',a_password='"+a_paswor+"',a_balance='"+a_balan+"' WHERE a_account='"+a_acc+"'";
            //执行SQL语句
            int row = sta.executeUpdate(str);
            System.out.println("影响行数" + row + "行");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            close(con, sta);
        }
    }



    public static void delete(String s_gender, String s_name) {
        Connection con = getConnection();
        Statement sta = null;
        try {
            sta = con.createStatement();
            String delet = "delete from t_student WHERE  s_gender='" + s_gender + "' or s_name='" + s_name + "'";
            int row = sta.executeUpdate(delet);
            System.out.println("影响行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, sta);
//            if (sta != null){
//                try {
//                    sta.close();
//                } catch (SQLException throwables) {
//                    throwables.printStackTrace();
//                }
//            }
//
//            if (con !=null){
//                try {
//                    con.close();
//                } catch (SQLException throwables) {
//                    throwables.printStackTrace();
//                }
//            }
        }
    }

    /*
     ** 查询
     */
    public static List select( ) {
        Connection con = getConnection();
        Statement sta = null;
        ResultSet rs = null;
        List useList =new ArrayList();
        try {

            sta = con.createStatement();
            String str = "select * from t_atm";
            rs = sta.executeQuery(str);
            while (rs.next()) {
                Uesr uesr =new Uesr();
                uesr.setAccount(rs.getString(1));
                uesr.setPassword(rs.getString(2));
                uesr.setBalance(rs.getInt(3));
                useList.add(uesr);
                System.out.println(useList);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (sta != null) {
                try {
                    sta.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
            if (con!=null){
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return (ArrayList)useList;
    }


    /*
     **封装coles 关闭流
     */
    public static void close(Connection con, Statement statement) {
        if (statement != null) {
            try {
                statement.close();
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }




}
